package ldh.im.fxgui.rpc.router;

import ldh.im.dto.base.OkResp;
import ldh.im.dto.business.base.CollectCreateReq;
import ldh.im.dto.business.base.DelReq;
import ldh.im.dto.business.base.FeedbackCreateReq;
import ldh.im.fxbase.dao.ImCollectDao;
import ldh.im.fxbase.data.pojo.ImCollect;
import ldh.im.fxbase.data.pojo.status.ImCollectStatus;
import ldh.im.fxbase.service.CollectService;
import ldh.im.fxbase.service.FeedbackService;
import ldh.im.fxbase.util.BeanFactory;
import ldh.im.fxgui.util.UiUtil;
import ldh.rpc.util.JsonUtil;
import ldh.rpc.Rpc;
import ldh.rpc.RpcMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

@Rpc
public class BaseConsumerRouter {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseConsumerRouter.class);

    private CollectService collectService = BeanFactory.getInstance().getBean(CollectService.class);
    private FeedbackService feedbackService = BeanFactory.getInstance().getBean(FeedbackService.class);
    private ImCollectDao collectDao = BeanFactory.getInstance().getBean(ImCollectDao.class);

    @RpcMapper(path="serverRedirectCollectCreate", asynHandle = true)
    public OkResp receiveCollect(CollectCreateReq collectCreateReq) throws Exception {
        LOGGER.info("收到服务端收藏事件 receive collect create ：{}", JsonUtil.toJson(collectCreateReq));
        String userId = UiUtil.getLoginImUser().getUserId();
        collectService.doCollectBusiness(userId, collectCreateReq.getCollectInfoItems());
        return new OkResp();
    }

    @RpcMapper(path="serverRedirectCollectDelete", asynHandle = true)
    public OkResp receiveCollectDelete(DelReq delReq) throws Exception {
        LOGGER.info("收到服务端收藏删除事件 receive collect delete ：{}", JsonUtil.toJson(delReq));
        String userId = UiUtil.getLoginImUser().getUserId();
        ImCollect collect = collectDao.getByKey(userId, delReq.getServerId());
        collect.setStatus(ImCollectStatus.disable);
        collect.setUpdateTime(new Date());
        collectDao.update(collect);
        return new OkResp();
    }

    /**
     * 接受到服务器的反馈信息
     * @param feedbackCreateReq
     * @return
     * @throws Exception
     */
    @RpcMapper(path="serverRedirectFeedbackCreate", asynHandle = true)
    public OkResp receiveFeedback(FeedbackCreateReq feedbackCreateReq) throws Exception {
        LOGGER.info("收到服务端反馈信息 receive feedback ：{}", JsonUtil.toJson(feedbackCreateReq));
        String userId = UiUtil.getLoginImUser().getUserId();
        feedbackService.doFeedbackCreateBusiness(userId, feedbackCreateReq);
        return new OkResp();
    }
}
